package com.e3roid.examples;

import java.util.ArrayList;

import android.widget.Toast;

import com.e3roid.E3Activity;
import com.e3roid.E3Engine;
import com.e3roid.E3Scene;
import com.e3roid.drawable.sprite.AnimatedSprite;
import com.e3roid.drawable.texture.TiledTexture;
import com.e3roid.drawable.tmx.TMXException;
import com.e3roid.drawable.tmx.TMXLayer;
import com.e3roid.drawable.tmx.TMXProperty;
import com.e3roid.drawable.tmx.TMXTile;
import com.e3roid.drawable.tmx.TMXTiledMap;
import com.e3roid.drawable.tmx.TMXTiledMapLoader;
import com.e3roid.event.SceneUpdateListener;
import com.e3roid.util.Debug;

public class TMXTiledMapExample extends E3Activity implements SceneUpdateListener {

	private final static int WIDTH  = 480;
	private final static int HEIGHT = 320;
	
	private TiledTexture dogTexture;
	private AnimatedSprite dogSprite;
	
	private ArrayList<AnimatedSprite.Frame> runningFrames;
	private TMXTiledMap map;
	private ArrayList<TMXLayer> mapLayers;
	
	private int dogX;
	private int dogY;
	private int mapStartX = 0;

	@Override
	public E3Engine onLoadEngine() {
		E3Engine engine = new E3Engine(this, WIDTH, HEIGHT);
		engine.requestFullScreen();
		engine.requestLandscape();
		return engine;
	}

	@Override
	public E3Scene onLoadScene() {
		E3Scene scene = new E3Scene();
		scene.registerUpdateListener(100, this);
		
		int tileHeight = 0;
		if (mapLayers != null) {
			for (TMXLayer layer : mapLayers) {
				// get the tile height.
				tileHeight = layer.getTileHeight();
				// determine scene range of the layer.
				// layer will skip drawing the tile that is out of the scene.
				layer.setSceneSize(getWidth(), getHeight());
				// layer can be looped.
				layer.loop(true);
				// add layer to the scene.
				scene.getTopLayer().add(layer);
			}
		} else {
			Toast.makeText(this, "Failed to load TMX map.", Toast.LENGTH_LONG).show();
		}
		
		dogX = (int)((getWidth()  - dogTexture.getTileWidth())  * 0.25);
		dogY = getHeight() - tileHeight - dogTexture.getTileHeight();
				
		// Start animation with 500msec, infinite loop.
		// Add animation frames to dog.
		dogSprite = new AnimatedSprite(dogTexture, dogX, dogY);
		dogSprite.animate(250, runningFrames);
		scene.getTopLayer().add(dogSprite);
		
		scene.setBackgroundColor(0f, 0f, 0f, 1);
		return scene;
	}

	@Override
	public void onLoadResources() {
		// 34x42 pixel sprite with 1px border and (0,0) tile.
		dogTexture  = new TiledTexture("dog.png", 34, 42, 0, 0, 1, this);
		
		// setup running animation
		runningFrames = new ArrayList<AnimatedSprite.Frame>();
		runningFrames.add(new AnimatedSprite.Frame(0, 0));
		runningFrames.add(new AnimatedSprite.Frame(1, 0));
		
		// get the map from TMX map file.
		try {
			TMXTiledMapLoader mapLoader = new TMXTiledMapLoader();
			map = mapLoader.loadFromAsset("orthoblocks.tmx", this);
			mapLayers = map.getLayers();
		} catch (TMXException e) {
			Debug.e(e);
		}
	}

	@Override
	public void onUpdateScene(E3Scene scene, long elapsedMsec) {
		if (mapLayers != null) {
			int jumpHeight = 20;
			for (TMXLayer layer : mapLayers) {
				// move the layer
				mapStartX += 10;
				layer.setPosition(mapStartX, 0);
				// reset mapStartX to avoid integer overflow.
				mapStartX = layer.getX();
				
				// get tile under the dog sprite.
				TMXTile underTile = layer.getTileFromPosition(
						dogSprite.getRealX() + (dogSprite.getWidth() / 2),
						dogSprite.getRealY() + dogSprite.getHeight() + jumpHeight + 5);
				
				if (TMXTile.isEmpty(underTile)) {
					// jump the dog at entering a empty tile.
					dogSprite.move(dogX, dogY - jumpHeight);
				} else {
					ArrayList<TMXProperty> props = map.getTileProperties(underTile.getGID());
					if (props != null) {
						for(TMXProperty prop : props) {
							if ("collidable".equals(prop.getName()) 
									&& "true".equals(prop.getValue())) {
								// land the dog on the collidable tile.
								dogSprite.move(dogX, dogY);
							}
						}
					}
				}
			}
		}
		
	}
}
